// pages/mbi/mbi.js
const Controller = require('../controllers/controllers.js').controller;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: 150,
    weight: 50,
    familyMemberId: '',
    id: '',
    bmi: '-',
    tag: '无',
    detail: null
  },
  goPage() {
    wx.navigateTo({ url: '/pages/contrastTable/contrastTable' });
  },
  //选择身高
  slideTrigger(e) {
    var that = this;
    that.setData({
      height: e.detail.curVal
    });
  },
  //选择身高
  slideWeightTrigger(e) {
    var that = this;
    that.setData({
      weight: e.detail.curVal
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.familyMemberId) {
      this.setData({
        familyMemberId: options.familyMemberId
      })
    }
    if (options.id) {
      this.setData({
        id: options.id
      })
      this.examdetail()
    }
    if(options.parentid){
      app.globalData.parentid=options.parentid
    }
  },

  //提交
  submit() {
    let data = {
      height: this.data.height,
      weight: this.data.weight
    }
    if (this.data.familyMemberId != '') {
      data.familyMemberId = this.data.familyMemberId
    }
    if(this.data.height==0){
      wx.showToast({
        title: '请输入或选择60-200范围的身高',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if(this.data.weight==0){
      wx.showToast({
        title: '请输入或选择30-150范围的体重',
        icon: 'none',
        duration: 2000
      })
      return
    }
    Controller.bmiExam(data).then(res => {
      if (res.code == 0) {
        let data = res.data.baseData
        let bmi = data.bmi
        let tag = data.title
        this.setData({
          bmi,
          tag
        })
      }
    })
  },

  examdetail() {
    Controller.examdetail({
      healthReportId: this.data.id
    }).then(res => {
      if (res.code == 0) {
        let data = res.data.baseData
        let bmi = data.bmi
        let tag = data.title
        this.setData({
          bmi,
          tag
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  onShareAppMessage: function (e) {
    let userInfo=wx.getStorageSync('userInfo') || '';
    var parentid='';
    if(userInfo){
      parentid=userInfo.id
    }
    var path="pages/selectMember/selectMember?parentid="+parentid;

    let title = '';
    let imageUrl = '';
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
  onShareTimeline: function(res){
    let userInfo=wx.getStorageSync('userInfo') || '';
    var parentid='';
    if(userInfo){
      parentid=userInfo.id
    }
    var path="pages/selectMember/selectMember?parentid="+parentid;

    let title = '';
    let imageUrl = '';
    let obj = {
      title,
      imageUrl,
      path,
      success: function (res) {},
      fail(err) {}
    }
    return obj
  },
})